package com.hundred.item.manager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hundred.item.dao.ItemCompareDAO;
import com.hundred.item.dao.ItemDao;
import com.hundred.item.dao.dataobject.ItemCompareDO;
import com.hundred.item.dao.dataobject.ItemDO;
import com.hundred.item.dao.dataobject.ItemReqDO;
import com.hundred.item.manager.ItemCompareManager;
import com.hundred.item.manager.ItemManager;

public class ItemCompareManagerImpl implements ItemCompareManager {

	private ItemCompareDAO itemCompareDAO;
	
	private ItemDao itemDao;
	
	@Autowired
	private ItemManager itemManager;
	
	private static final Logger logger = LoggerFactory.getLogger(ItemCompareManagerImpl.class);

	private Map<Integer, ArrayList<Long>> itemListMap = new HashMap <Integer,ArrayList<Long>>();
	
	public boolean addNewItemCompareModel(ItemCompareDO itemCompareDO) {

		this.itemCompareDAO.insertCompareModel(itemCompareDO);
		return true;
		
	}

	public List<ItemCompareDO> selectSingleUserCompareResult(long userId) {
		
		return this.itemCompareDAO.getCompareModelByUserId(userId);
	}

	public ItemCompareDAO getItemCompareDAO() {
		return itemCompareDAO;
	}

	public void setItemCompareDAO(ItemCompareDAO itemCompareDAO) {
		this.itemCompareDAO = itemCompareDAO;
	}

	public void init(){
		List<ItemDO> itemDOList = itemDao.getInitCacheItems(0);
		for(ItemDO itemDO:itemDOList){
			if(itemListMap.get(itemDO.getTagId())==null){
				ArrayList<Long> itemIdList= new ArrayList<Long> ();
				itemIdList.add(itemDO.getItemId());
				itemListMap.put(itemDO.getTagId(), itemIdList);
			}
			else{
				ArrayList<Long> itemIdList = itemListMap.get(itemDO.getTagId());
				itemIdList.add(itemDO.getItemId());
				itemListMap.put(itemDO.getTagId(), itemIdList);
			}
		}
	}
	
	public List<ItemDO> selectCompareItems(int num,int tagId) {

		int totalNum = itemListMap.get(tagId).size()-2;
		
		long item1 = itemListMap.get(tagId).get((int)(Math.random()*(totalNum)));
		
		long item2 = itemListMap.get(tagId).get((int)(Math.random()*(totalNum)));
		
		ItemReqDO itemReq= new ItemReqDO();
		itemReq.setItemId(item1);
		itemReq.setTagId(tagId);
		ItemReqDO itemReq2= new ItemReqDO();
		itemReq2.setItemId(item2);
		itemReq2.setTagId(tagId);
		ItemDO leftItem = itemManager.getItemById(itemReq);
		
		ItemDO rightItem = itemManager.getItemById(itemReq2);
		
		if(leftItem!=null && rightItem!=null){
			
			List<ItemDO> itemList = new ArrayList<ItemDO>();
			itemList.add(leftItem);
			itemList.add(rightItem);
			logger.error("Two-Item-Found");
			return itemList;
		}
		itemReq.setItemId(12);
		leftItem = itemManager.getItemById(itemReq);
		itemReq2.setItemId(17);
		rightItem = itemManager.getItemById(itemReq2);
		
		if(leftItem!=null && rightItem!=null){
			
			List<ItemDO> itemList = new ArrayList<ItemDO>();
			itemList.add(leftItem);
			itemList.add(rightItem);
			logger.error("Two-Item-Found2");
			return itemList;
		}
		logger.error("sorry!!!!!!!!!!!!");
		return new ArrayList<ItemDO>();
	}
	
	public ItemManager getItemManager() {
		return itemManager;
	}

	public void setItemManager(ItemManager itemManager) {
		this.itemManager = itemManager;
	}

	public ItemDao getItemDao() {
		return itemDao;
	}

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}


}
